#include "../include/MyClient.h"
#include "../include/menu.h"
#include "../../dataParser/include/dataParser.h"
#include <cjson/cJSON.h>
#include <cstring>

MyClient::MyClient(int mode, int port, char *ip_addr, int timeout) : Client(mode, port, ip_addr, timeout)
{
    this->username_ = new char[1024];
}

int MyClient::login()
{
    char *msg = new char[1024];
    int value = 0;

    if (fgets(msg, 1024, stdin) != NULL)
    {
        msg[strlen(msg) - 1] = 0;
        if (strcmp(msg, "0") == 0)
        {
            exit(0);
        }
        if (strcmp(msg, "1") == 0)
        {
            this->_login();
        }
    }
    return 0;
}

int MyClient::_login()
{
    char *username = new char[1024];
    char *password = new char[1024];

    cout << "please enter your username:" << endl;
    fgets(username, 1024, stdin);
    username[strlen(username) - 1] = 0;
    strcpy(this->username_, username);
    cout << "please enter your password:" << endl;
    fgets(password, 1024, stdin);
    password[strlen(password) - 1] = 0;
    cJSON *j = cJSON_CreateObject();
    encodeLoginInfo(username, password, j);
    char *data = cJSON_Print(j);
    this->sendMsg(data, strlen(data));
    cJSON_Delete(j);
}